package shujia.day07.ketang;

/*
    Object类中的成员方法：
        public boolean equals(Object obj) Object类中的equals方法底层是使用==比较的，比较的是地址值
            如果要比较内容值的话，需要在对象的类中重写该方法
        protected void finalize()  做垃圾回收的
        protected Object clone() 浅拷贝
            Cloneable接口中什么方法都没有，像这样的接口称之为标记接口


 */

import java.util.Objects;

class Demo {
    int a = 10;
}

class Student2 implements Cloneable {
    String name;
    int age;
    Demo demo;

    public Student2() {
    }

    public Student2(String name, int age, Demo demo) {
        this.name = name;
        this.age = age;
        this.demo = demo;
    }

    public Student2(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student2{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (o == null || getClass() != o.getClass()) return false;
        Student2 student2 = (Student2) o;
        return age == student2.age && Objects.equals(name, student2.name);
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}

public class ObjectDemo3 {
    public static void main(String[] args) throws Exception {
        Student2 s1 = new Student2("张成阳", 18, new Demo());
//        Student2 s2 = new Student2("张成阳", 18);
//
//        System.out.println(s1.equals(s2)); // s1和s2都是分别new出来的，地址值肯定不一样

        // 从比较地址值变成比较成员变量值
        System.out.println("-----------------------------------");
        Object o1 = s1.clone();
        System.out.println("o1: " + o1);
        System.out.println("s1: " + s1);
        System.out.println(s1 == o1);

        Student2 s2 = (Student2) o1;
        System.out.println(s1.demo == s2.demo);


    }
}
